package com.neuralnoise;

import java.util.Collection;

import org.apache.log4j.Logger;

import com.neuralnoise.geo.GeoUtils.ServiceType;
import com.neuralnoise.hibernate.HibernateSessionFactory;
import com.neuralnoise.hibernate.geo.GeoLocation;
import com.neuralnoise.hibernate.geo.GeoLocationDAO;
import com.neuralnoise.hibernate.geo.GeoLocationId;

public class GeoDB extends AbstractDB {

	private Logger logger = Logger.getLogger(this.getClass());
	
	private GeoDB() { }
	
	public static void clean() {
		HibernateSessionFactory.getSession().beginTransaction();
		GeoLocationDAO gdao = new GeoLocationDAO();
		Collection<GeoLocation> gs = gdao.findAll();
		for (GeoLocation g : gs) {
			gdao.delete(g);
		}
		HibernateSessionFactory.getSession().getTransaction().commit();
	}

	public static GeoLocation getLocation(ServiceType service, String address, String language) {
		HibernateSessionFactory.getSession().beginTransaction();
		GeoLocationDAO gdao = new GeoLocationDAO();
		GeoLocationId gid = new GeoLocationId(service, address, language);
		GeoLocation g = gdao.findById(gid);
		HibernateSessionFactory.getSession().getTransaction().commit();
		return g;
	}
	
	public static GeoLocation addLocation(ServiceType service, String address, String language, String name,
			Double altitude, Double latitude, Double longitude) {
		HibernateSessionFactory.getSession().beginTransaction();
		GeoLocationDAO gdao = new GeoLocationDAO();
		GeoLocationId gid = new GeoLocationId(service, address, language);
		GeoLocation g = new GeoLocation(gid, name, altitude, latitude, longitude);
		gdao.merge(g);
		HibernateSessionFactory.getSession().getTransaction().commit();
		return g;
	}
	
	public static GeoLocation addLocation(GeoLocation g) {
		HibernateSessionFactory.getSession().beginTransaction();
		GeoLocationDAO gdao = new GeoLocationDAO();
		gdao.merge(g);
		HibernateSessionFactory.getSession().getTransaction().commit();
		return g;
	}
	
}
